package cs.ubbcluj.ro.storage;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import cs.ubbcluj.ro.model.Article;
 

@Stateless
public class ArticleDao {
    // Injected database connection:
	//(unitName="ArticleDao")
    @PersistenceContext(unitName="articleDao")
    private EntityManager em;
    
    public ArticleDao()
    {
        int z=0;
    	EntityManagerFactory emf = Persistence.createEntityManagerFactory("articleDao");
        em = emf.createEntityManager();
    }
 
    // Stores a new guest:
    public void persist(Article article) {
        em.persist(article);
    }
 
    // Retrieves all the guests:
    public List<Article> getAllArticles() {
        TypedQuery<Article> query = em.createQuery(
            "SELECT g FROM Article g ORDER BY g.id", Article.class);
        return query.getResultList();
    }
}